package
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	[SWF(width="800", height="600", backgroundColor="#000000" )]
	public class PreLoader extends Sprite
	{
		private var mainAppURL:String = "Main.swf";
		private var _flashVars:Object;
		private var _mainLoader:Loader;
		private var _mainApp:IStartInterFace;
		public function PreLoader()
		{
			super();
			_flashVars = this.loaderInfo.parameters;
			
			_mainLoader = new Loader();
			_mainLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,onMainAppLoadedHandle);
			_mainLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onMainLoadedErrorHandle);
			_mainLoader.load(new URLRequest(mainAppURL));
		}
		
		private function onMainLoadedErrorHandle(event:IOErrorEvent):void
		{
			destroyEvents();
		}
		
		private function onMainAppLoadedHandle(event:Event):void
		{
			destroyEvents();
			
			this.addChild(event.target.content);
			IStartInterFace(event.target.content).run(_flashVars);
			
		}
		
		
		private function destroyEvents():void
		{
			_mainLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onMainAppLoadedHandle);
			_mainLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onMainLoadedErrorHandle);
		}
	}
}